$(document).ready(function(){
base_url = document.URL.slice(0,document.URL.indexOf("index.jsp"));
var user = getCookie('loginInfo');
var islogin = getCookie('isLogin');
if(islogin!=''&&user != ''){//已经登录了，只用刷新	
$("#userInfo").html("<div><h2>User Information</h2><div class='introduction_div'><div style='float: left; margin-right: 15px; margin-left: 20px; display: inline;'><img src='http://www.prcqq.cn/upimg/userup/0804/0F02F91a4.jpg' alt='个人中心' width='50' height='50'></div>"+
        			"<p>Hello ,<font color='blue'>"+user.split("|")[0]+"</font></p><a href='#'><span>你有0条消息</span></a><br><div style='float: center; margin-right: 15px; margin-left: 80px;'><a href='#'><img src='http://img1.2345.com/duoteimg/techImg/201204/duote_14_09-44-55.jpg' alt='20092083' width='20' height='20'>"+
					"<span>个人中心</span></a><br><a href='#'><span>我的购物车</span></a><br><a href='#' onclick='logout();'><span>注销</span></a><br></div><div>&nbsp;</div></div></div>");
}
else //还未登录，发送cookie登录请求并刷新
 {
	
 }
});
   function userLogin(){
	    base_url = document.URL.slice(0,document.URL.indexOf("index.jsp"));
	  	var userName = escape($('#username').val());
	  	var password = escape($('#password').val());
	  	if(userName==''){
	  	var info = '提示：<br /><h3>你输入的用户名不能为空！<h3 />';
		$.jBox.info(info,'警告');
		return ;
	  	}
	  	if(password ==''){
	  	var info = '提示：<br /><h3>你输入的密码不能为空！<h3/>';
		$.jBox.info(info,'警告');
		return ;
	  	}
	  	$.ajax({
				type:"post",
				url: base_url+"web/login.action",
				data:{userName:userName,password:password},
				dataType:"JSON",
				success:function(json){
					if(json.flag=='0'){
						$('#loginMess').html("<span> <font color='red'>"+json.message+"</color></span>");
				}
				else{
					$("#userInfo").html("<div><h2>用户信息</h2><div class='introduction_div'><div style='float: left; margin-right: 15px; margin-left: 20px; display: inline;'><img src='http://www.prcqq.cn/upimg/userup/0804/0F02F91a4.jpg' alt='个人中心' width='50' height='50'></div>"+
        			"<p>Hello ,<font color='blue'>"+json.userName+"</font></p><a href='#'><span>你有0条消息</span></a><br><div style='float: center; margin-right: 15px; margin-left: 80px;'><a href='#'><img src='http://img1.2345.com/duoteimg/techImg/201204/duote_14_09-44-55.jpg' alt='20092083' width='20' height='20'>"+
					"<span>个人中心</span></a><br><a href='#'><span>我的购物车</span></a><br><a href='#'onclick='logout();'><span>注销</span></a><br></div><div>&nbsp;</div></div></div>");
				}
			},
			error:function()
			 {
				$.jBox.info("提示：<br /><h3>载入失败，请重新刷新页面！<h3/><br>如果依然不能解决，请告诉我们，shopOnlie@swust.edu.cn",'提示');
			 }
	});
}
//读取cookie   
function getCookie(c_name){
	//alert(document.cookie);
if (document.cookie.length>0)
 {
  c_start=document.cookie.indexOf(c_name + "=")
  if (c_start!=-1)
   { 
    c_start=c_start + c_name.length+1 
    c_end=document.cookie.indexOf(";",c_start)
    if (c_end==-1) c_end=document.cookie.length
    return unescape(document.cookie.substring(c_start,c_end))
   } 
  }
return ""
}
//删除cookie
function delCookie(name)
{
var date=new Date(); 
date.setTime(date.getTime()-10000); 
document.cookie=name+"=; expires="+date.toGMTString(); 
//alert("删除成功");
}
function logout()
{
	var submit = function (v, h, f) {
    if (v == 'ok'){
        jBox.tip(v, 'info');
    	$.ajax({
				type:"post",
				url: base_url+"web/logout.action",
				data:{},
				dataType:"JSON",
				success:function(json){	
					$("#userInfo").html("<div style='margin-top:10px; ' id='userInfo'><h2>爱心屋登陆</h2><div class='introduction_div'><ul><li>用户名：</li><li><input name='username' id='username' /></li>"+
							"<li>密&nbsp;&nbsp;&nbsp;码：</li><li><input name='password' type='password' id='password' /></li><li><div id='login'><input id='logon' onclick='userLogin()' type='button'></input></div></li><li><div id='loginMess'></div></li></ul></div></div>");
					delCookie('swustAddPro');
			},
			error:function()
			 {
				$.jBox.info("提示：<br /><h3>载入失败，请重新刷新页面！<h3/><br>如果依然不能解决，请告诉我们，shopOnlie@swust.edu.cn",'提示');
			 }
	});    
  }
    else if (v == 'cancel')
        jBox.tip(v, 'info');
    return true; //close
};
$.jBox.confirm("你确定要退出登录吗？", "提示", submit);	
}